﻿using RTW.OA.Models;
using RTW.OA.Servers;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace RTW.OA.Controllers
{
    public class ComponentsController : Controller
    {
        private ComponentsServers componentsServers;

        public ComponentsController()
        {
            componentsServers = new ComponentsServers();
        }

        // GET: Components
        public ActionResult _AddUser(string id)
        {
            ViewBag.right = id ?? "";
            return View();
        }
        // GET: Components
        public ActionResult _AddUsers(string id)
        {
            ViewBag.right = id ?? "";
            return View();
        }

        public ActionResult GetjsonTreeData(string idstr = null)
        {
            var list = GetDepts(0, idstr);
            return Json(list, JsonRequestBehavior.AllowGet);
        }

        public List<TreeViewNode> GetDepts(int parentId, string idstr)
        {
            List<TreeViewNode> tlist = new List<TreeViewNode>();
            var list = componentsServers.GetDepts();
            var deptlist = list.FindAll(m => m.DEPT_PARENT == parentId);
            if (deptlist != null && deptlist.Count > 0)
            {
                foreach (var item in deptlist)
                {
                    TreeViewNode treeViewNode = new TreeViewNode();
                    treeViewNode.text = item.DEPT_NAME;
                    treeViewNode.id = "D" + item.DEPT_ID.ToString();
                    treeViewNode.icon = "glyphicon glyphicon-equalizer";
                    treeViewNode.state = new State { @checked = false };
                    treeViewNode.nodes = new List<TreeViewNode>();
                    var personlist = GetPerson(item.DEPT_ID, idstr);
                    if (personlist != null && personlist.Count > 0)
                    {
                        treeViewNode.nodes.AddRange(personlist);
                    }
                    var subdeptlist = GetDepts(item.DEPT_ID, idstr);
                    if (subdeptlist != null && subdeptlist.Count > 0)
                    {
                        treeViewNode.nodes.AddRange(subdeptlist);
                    }
                    if (!string.IsNullOrEmpty(idstr))
                    {
                        if (idstr.Contains("D" + item.DEPT_ID.ToString()))
                        {
                            treeViewNode.state = new State { @checked = true };
                        }

                    }

                    tlist.Add(treeViewNode);
                }
            }
            return tlist;

        }

        public List<TreeViewNode> GetPerson(int parentId, string idstr)
        {
            List<TreeViewNode> tlist = new List<TreeViewNode>();
            var list = componentsServers.GetUsers().FindAll(m => m.DEPT_ID == parentId);
            if (list != null && list.Count > 0)
            {
                foreach (var item in list)
                {
                    TreeViewNode treeViewNode = new TreeViewNode();
                    treeViewNode.text = item.TRUE_NAME;
                    treeViewNode.id = "U" + item.USER_ID.ToString();
                    treeViewNode.icon = "glyphicon glyphicon-user";
                    if (!string.IsNullOrEmpty(idstr))
                    {
                        if (idstr.Contains("U" + item.DEPT_ID.ToString()))
                        {
                            treeViewNode.state = new State { @checked = true };
                        }

                    }
                    tlist.Add(treeViewNode);
                }
            }
            return tlist;
        }

        public ActionResult GetjsonZTreeData(string idstr = null)
        {
            var list = GetZTreeDepts(0, idstr);
            return Json(list, JsonRequestBehavior.AllowGet);
        }

        public List<ZTreeNode> GetZTreeDepts(int parentId, string idstr)
        {
            List<ZTreeNode> tlist = new List<ZTreeNode>();
            var list = componentsServers.GetDepts();
            var deptlist = list.FindAll(m => m.DEPT_PARENT == parentId);
            if (deptlist != null && deptlist.Count > 0)
            {
                foreach (var item in deptlist)
                {
                    ZTreeNode treeViewNode = new ZTreeNode();
                    treeViewNode.name = item.DEPT_NAME;
                    treeViewNode.id = "D" + item.DEPT_ID.ToString();
                    treeViewNode.icon = "glyphicon glyphicon-equalizer";
                    treeViewNode.nocheck = true;
                    treeViewNode.open = true;
                    treeViewNode.children = new List<ZTreeNode>();
                    var personlist = GetZTreePerson(item.DEPT_ID, idstr);
                    if (personlist != null && personlist.Count > 0)
                    {
                        treeViewNode.children.AddRange(personlist);
                    }
                    var subdeptlist = GetZTreeDepts(item.DEPT_ID, idstr);
                    if (subdeptlist != null && subdeptlist.Count > 0)
                    {
                        treeViewNode.children.AddRange(subdeptlist);
                    }
                    //if (!string.IsNullOrEmpty(idstr))
                    //{
                    //    if (idstr.Contains("D" + item.DEPT_ID.ToString()))
                    //    {
                    //        treeViewNode.@checked = true;
                    //    }

                    //}

                    tlist.Add(treeViewNode);
                }
            }
            return tlist;

        }

        public List<ZTreeNode> GetZTreePerson(int parentId, string idstr)
        {
            List<ZTreeNode> tlist = new List<ZTreeNode>();
            var list = componentsServers.GetUsers().FindAll(m => m.DEPT_ID == parentId);
            if (list != null && list.Count > 0)
            {
                foreach (var item in list)
                {
                    ZTreeNode treeViewNode = new ZTreeNode();
                    treeViewNode.name = item.TRUE_NAME;
                    treeViewNode.id = "U" + item.USER_ID.ToString();
                    treeViewNode.icon = "glyphicon glyphicon-user";
                    if (!string.IsNullOrEmpty(idstr))
                    {
                        var idlist=idstr.Split(',');
                        if (idlist.Contains(item.USER_ID.ToString()))
                        {
                            treeViewNode.@checked = true;
                        }

                    }
                    tlist.Add(treeViewNode);
                }
            }
            return tlist;
        }

        public string GetUserName(string ids)
        {
            string names = string.Empty;
            var idlist = ids.Split(',');
            if (idlist != null && idlist.Count() > 0)
            {
                var list = componentsServers.GetUsers().Where(m => idlist.Contains(m.USER_ID.ToString()));
                names = string.Join(",", list.Select(m => m.TRUE_NAME));
            }
            
            return names;
        }
    }
}